package com.yoomiito.app.ui.my.angle

import android.Manifest
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.PointF
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.davemorrissey.labs.subscaleview.ImageSource
import com.davemorrissey.labs.subscaleview.ImageViewState
import com.yoomiito.app.Constant
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.utils.*
import kotlinx.android.synthetic.main.act_sign_agreement.*
import kotlinx.android.synthetic.main.title_bar.*
import org.greenrobot.eventbus.EventBus
import java.io.File

class MyAngleAgreementActivity :BaseActivity<MyAngleAgreementPresenter>(){

    companion object{
        fun start(context: Context,url:String){
            val intent = Intent(context,MyAngleAgreementActivity::class.java)
            intent.putExtra(Constant.KEY,url)
            context.startActivity(intent)
        }
    }
    private lateinit var url:String
    private lateinit var mBitmap:Bitmap
    override fun getLayoutId(): Int = R.layout.act_my_angel_agreement

    override fun newP(): MyAngleAgreementPresenter = MyAngleAgreementPresenter(App.mAppConfig)

    override fun initData(savedInstanceState: Bundle?) {
        url = intent.getStringExtra(Constant.KEY)
        tv_center.text = "我的合同"
        tv_center.paint.isFakeBoldText = true
        tv_right.text = "下载"
        tv_right.visibility = View.VISIBLE
        tv_right.setOnClickListener {
            if (Build.VERSION.SDK_INT >= 23) {
                requestPermission()
            } else {
                loadPic()
            }
        }
        iv_back_left.setOnClickListener {
           back()
        }
        Glide.with(this).load(url).downloadOnly(object : SimpleTarget<File>() {
            override fun onResourceReady(resource: File, transition: Transition<in File>?) {
                mBitmap = ImageUtils.getBitmapByFile(resource)
                dismissLoading()
                agreementIv.setImage(ImageSource.uri(Uri.fromFile(resource)), ImageViewState(1.0f, PointF(0f, 0f), 0))
            }
        })
    }

    fun requestPermission() {
        AllUtils.requestPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe { aBoolean -> if (aBoolean!!) loadPic() }
    }

    private fun loadPic() {
        ImageLoader.getInstance().downloadPic(this, mBitmap)
        Toast.makeText(context, "保存成功", Toast.LENGTH_SHORT).show()
    }

    override fun onBackPressed() {
        super.onBackPressed()
        back()
    }

    private fun back(){
        val eventMessage = EventMessage()
        eventMessage.eventType = AllCode.ANGLE_AGREEMENT_SIGN
        EventBus.getDefault().post(eventMessage)
        finish()
    }
}